﻿// JavaScript Document

var musicPlayState = false;
var $context;
var initChessBoard = function(){
	"use strict";
	win = 0;
	chessNum = 0;
	for(var i=0;i<15;i++){
		chessBoardArray[i] = new Array(15);
		for(var j=0;j<15;j++){
			chessBoardArray[i][j] = 0;
		}
	}
	drawChessBoard();
};
var drawChessBoard = function(){
	"use strict";
	$context.clearRect(0,0,540,540);
	$context.strokeStyle="#DD0D10";
	$context.beginPath();
	for(var i=0;i<15;i++){
		$context.moveTo(18+i*36,18);
		$context.lineTo(18+i*36,522);
		$context.moveTo(18,18+i*36);
		$context.lineTo(522,18+i*36);
	}
	$context.stroke();
	$context.closePath();
};
var musicClick = function(){
	"use strict";
	if(!musicPlayState){
		musicPlayState = true;
		$('#media')[0].play();
		$('#music').addClass('music-on');
	}
	else{
		musicPlayState = false;
		$('#media')[0].pause();
		$('#music').removeClass('music-on');
	}
};
var drawChess = function(y,x,flag){
	"use strict";
	$context.beginPath();
    $context.arc(18 + x * 36, 18 + y * 36, 15, 0, 2 * Math.PI);
    $context.closePath();
    var gradient = $context.createRadialGradient(18 + x * 36 + 2, 18 + y * 36 - 2, 15, 18 + x * 36 + 2, 18 + y * 36 - 2, 0);
    if (flag === 1) {
        gradient.addColorStop(0, "#0A0A0A");
        gradient.addColorStop(1, "#636766");
    } else {
        gradient.addColorStop(0, "#D1D1D1");
        gradient.addColorStop(1, "#F9F9F9");
    }
    $context.fillStyle = gradient;
    $context.fill();
};
$(document).ready(function(){
	"use strict";
	$context =$("#drawCanvas")[0].getContext("2d");
	initChessBoard();
	$("#drawCanvas")[0].onclick = function(e){
		if(win!==0)
			return;
		var x = Math.floor(e.offsetX / 36);
		var y = Math.floor(e.offsetY / 36);
		if (!IsIn(x) || !IsIn(y) || (chessBoardArray[y][x] !== 0)){
			return;
		}
		drawChess(y,x,userFlag);
		onStep(y,x);
		if(win===userFlag){
			setTimeout("alert('你赢了');initChessBoard();",2000);
		}else if(win===AIFlag){
			drawChess(selectY,selectX,AIFlag);
			setTimeout("alert('你输了');initChessBoard();",2000);				
		}else{
			drawChess(selectY,selectX,AIFlag);
		}
};
});